package hibernatelogic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import orm.Items;
import orm.Transactions;

import utils.HibernateUtil;

/**
 * 
 * The DAO class for table TRANSACTIONS
 * @author Igor Petrov
 *
 */
public class HibTransactions {
	
	public HibTransactions(){}
	
	/**
	 * select * from transactions;
	 * @throws SQLException
	 */
	public Collection getAllTransactions() throws SQLException {
		Session session = null;
		List transactions = new ArrayList<Transactions>();
		try {
			session = hibUtil.getSessionFactory().openSession();
			transactions = session.createCriteria(Transactions.class).list();
		} catch (Exception e) {
			loggerHibTransactions.error("getAllTransactions failed!!!", e);
		} finally {
			if (session != null && session.isOpen()) {
		        session.close();
		      }
		}
		return transactions;
	}
	
	/**
	 * insert into transactions values([transaction])
	 * @param transaction
	 * @throws SQLException
	 */
	public void addTransaction(Transactions transaction) throws SQLException {
		session = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.save(transaction);
			session.getTransaction().commit();
		} catch (Exception e) {
			loggerHibTransactions.error("addTransaction failed!!!", e);
			session.getTransaction().rollback();
		} finally {
			if (session != null && session.isOpen()){
				session.close();
			}
		}
	}
	
	/**
	 * update transactions set 'transactions_param_1' = transactions_param_1 where 'transactions_param_2' = transactions_param_2
	 * @param transaction
	 * @throws SQLException
	 */
	public void updateTransaction (Transactions transaction) throws SQLException {
		session = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.update(transaction);
			session.getTransaction().commit();
		} catch (Exception e) {
			loggerHibTransactions.error("updateTransaction failed!!!", e);
			session.getTransaction().rollback();
		} finally {
			if(session != null && session.isOpen()){
				session.close();
			}
		}
	}
	
	/**
	 * select * from transactions where transaction_id = 'tr_id'
	 * @param tr_id
	 * @return
	 * @throws SQLException
	 */
	public Transactions getTransactionByID(int tr_id) throws SQLException {
		session = null;
		Transactions transaction = null;
		try {
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			transaction = (Transactions) session.get(Transactions.class, tr_id);
		} catch (Exception e) {
			loggerHibTransactions.error("getTransactionByID failed!!!", e);
		} finally {
			if(session != null && session.isOpen()){
				session.close();
			}
		}
		return transaction;
	}
	
	/**
	 * select * from transactions where status = 'N'
	 * @return
	 * @throws SQLException
	 */
	public Collection getNotApprovedTransactions() throws SQLException {
		Session session = null;
		List transactions = new ArrayList<Transactions>();
		try {
			session = hibUtil.getSessionFactory().openSession();
			Query query = session.createQuery("from Transactions where status = 'N'");
			transactions = query.list();
		} catch (Exception e) {
			loggerHibTransactions.error("getAllTransactions failed!!!", e);
		} finally {
			if (session != null && session.isOpen()) {
		        session.close();
		      }
		}
		return transactions;
	}
	
	public void deleteTransactions(Transactions transaction) throws SQLException {
		try {	
			session = null;
			session = hibUtil.getSessionFactory().openSession();
			session.beginTransaction();
			session.delete(transaction);
			session.getTransaction().commit();
		} catch (Exception e) {
			loggerHibTransactions.error("deleteTransaction  failed!!!", e);
			session.getTransaction().rollback();
		} finally {
			if(session != null && session.isOpen()){
				session.close();
			}
		}
	}
	
	HibernateUtil hibUtil = new HibernateUtil();
	private Session session = null;
	private static final Logger loggerHibTransactions = Logger.getLogger(HibTransactions.class); 
}
